package com.chenyue.cm.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.user.dao.JdAppUserMapper;
import com.chenyue.cm.user.dao.JdUserOpinionMapper;
import com.chenyue.cm.user.dao.JdUserRelationMapper;
import com.chenyue.cm.user.domain.JdAppUser;
import com.chenyue.cm.user.domain.JdUserOpinion;
import com.chenyue.cm.user.domain.JdUserRelation;
import com.chenyue.cm.user.service.JdUserRelationService;
import com.chenyue.cm.user.vo.UserRelation;
import com.chenyue.cm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * @author chenyue-cm
 */
@Service(value = "JdUserRelationService")
public class JdUserRelationServiceImpl extends BaseServiceImpl<JdUserRelation> implements JdUserRelationService {

    private final JdUserRelationMapper jdUserRelationMapper;

    private final JdAppUserMapper jdAppUserMapper;

    private final JdUserOpinionMapper jdUserOpinionMapper;

    public JdUserRelationServiceImpl(@Qualifier("jdUserRelationMapper") JdUserRelationMapper jdUserRelationMapper,
                                     @Qualifier("jdAppUserMapper") JdAppUserMapper jdAppUserMapper,
                                     @Qualifier("jdUserOpinionMapper") JdUserOpinionMapper jdUserOpinionMapper) {
        this.jdUserRelationMapper = jdUserRelationMapper;
        this.jdAppUserMapper = jdAppUserMapper;
        this.jdUserOpinionMapper = jdUserOpinionMapper;
    }

    @Override
    @Resource(name = "jdUserRelationMapper")
    public void setBaseMapper(LcBaseMapper<JdUserRelation, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }

    /**
     * 查询关注人数
     *
     * @param id 用户id
     * @return 用户关注的人数
     */
    @Override
    public int getRelationCount(Integer id) {
        return jdUserRelationMapper.selectCount(new QueryWrapper<JdUserRelation>().lambda().eq(JdUserRelation::getSourceUser, id));
    }

    @Override
    public List<UserRelation> relationList(LambdaQueryChainWrapper<JdUserRelation> eq, Integer type) {
        //返回列表
        List<UserRelation> result = new ArrayList<>();
        List<JdUserRelation> list = eq.list();
        for (JdUserRelation jdUserRelation : list) {
            JdAppUser jdAppUser = jdAppUserMapper.selectById(type == 1 ? jdUserRelation.getRelationUser() : jdUserRelation.getSourceUser());
            UserRelation userRelation = new UserRelation();
            userRelation.setId(jdUserRelation.getId());
            userRelation.setRelationUserId(jdUserRelation.getRelationUser());
            userRelation.setUserName(jdAppUser.getUserName());
            userRelation.setUserNum(jdAppUser.getUserNum());
            userRelation.setRole(jdUserRelation.getRole());
            userRelation.setHeadImg(jdAppUser.getHeadImg());
            userRelation.setFansNum(lambdaQuery().eq(JdUserRelation::getRelationUser, jdUserRelation.getRelationUser()).count());
            result.add(userRelation);
        }
        return result;
    }

    @Override
    public Integer userFeedback(String mobile, String content, String image, Integer userId) {
        JdUserOpinion userOpinion = new JdUserOpinion();
        userOpinion.setMobile(mobile);
        userOpinion.setContent(content);
        if (null != image && image.length() > 0) {
            if ((image.lastIndexOf(",") + 1) == image.length()) {
                image = image.substring(0, image.length() - 1);
            }
            userOpinion.setImage(image);
        }
        userOpinion.setCu(userId);
        userOpinion.setCt(DateUtils.getToday());
        userOpinion.setIsRead(0);
        return jdUserOpinionMapper.insert(userOpinion);
    }

    /**
     * 我的粉丝
     *
     * @param id 用户id
     * @return 关注我的数量
     */
    @Override
    public Integer getFansCount(Integer id) {
        return lambdaQuery().eq(JdUserRelation::getRelationUser, id).count();
    }

    /**
     * 是否关注
     *
     * @param id         当前登录用户
     * @param targetUser 目标用户
     * @return 1已关注 2未关注
     */
    @Override
    public Integer isRelation(Integer id, Integer targetUser) {
        return lambdaQuery().eq(JdUserRelation::getSourceUser, id).eq(JdUserRelation::getRelationUser, targetUser).eq(JdUserRelation::getRole, 1)
                .last(" limit 1").one() == null ? 2 : 1;
    }

    /**
     * 删除
     *
     * @param id 主键
     */
    @Override
    public void delete(Integer id) {
        jdUserRelationMapper.deleteById(id);
    }
}
